package com.fr.lintcode;

import org.junit.Test;

/**
*作者：furong
*日期：2017年1月16日
*时间：下午5:57:23
*/
public class Q93 {
    /**
     * @param root: The root of binary tree.
     * @return: True if this Binary tree is Balanced, or false.
     */
    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        if ((root.left == null && (root.right != null && (root.right.left != null || root.right.right != null)))
                || (root.right == null && (root.left != null && (root.left.left != null || root.left.right != null)))) {
            return false;
        }
        return isBalanced(root.left) & isBalanced(root.right);
    }

    @Test
    public void testA() {
        TreeNode root = new TreeNode(0);
        TreeNode r1 = new TreeNode(1);
        TreeNode r2 = new TreeNode(2);
        TreeNode r3 = new TreeNode(3);
        TreeNode r4 = new TreeNode(4);
        TreeNode r5 = new TreeNode(5);
        TreeNode r6 = new TreeNode(5);
        TreeNode r7 = new TreeNode(5);

        root.left = r1;
        root.right = r2;
        r1.left = r3;
        r1.right = r5;
        r3.left = r6;
        r3.right = r7;
        System.out.println(isBalanced(root));
    }
}
